package leeon.words;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

public class DictIndex {
	
	//无key,表示该词
	//有key,value为"",表示有词但已经到末尾
	//有key,value为"...",表示只有该词已经到末尾
	//有key,value为新的map,表示有词且还未到末尾
	//有key,value为新的map,map中有key为"",表示有多个词,其中一个已经到末尾,一个没有到末尾
	private Map<Character, Object> dictIndex = new HashMap<Character, Object>();
	
	public Map<Character, Object> getDictIndex() {
		return dictIndex;
	}

	public DictIndex(String dictFilePath) throws IOException {
		this(new File(dictFilePath));
	}
	
	public DictIndex(File dictFile) throws IOException {
		this(new FileInputStream(dictFile));
	}
	
	public DictIndex(InputStream input) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(input, "UTF8"));
		String line = null;
		while ((line = in.readLine()) != null) {
			//排除空行
			line = line.trim();
			if (line.length() == 0) continue;
			
			//调用递归建立索引的过程
			createIndexNode(line, 0, dictIndex);
		}
		in.close();
	}
	
	//递归建立索引的过程
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void createIndexNode(String line, int i, Map<Character, Object> node) {
		//如果当前行字符串的列标已经到末尾
		if (i == line.length()) {
			//表示有该词且已经到末尾
			node.put(new Character(((char)0)), "");
			return;
		}
		
		//取出需要处理的char开始判断逻辑
		//下移一个index
		char c = line.charAt(i++);
		
		//无key对应，该级别已经无后续对应的词
		if (!node.containsKey(c)) {
			//将后续词放入，形成c : "..."或者""的情况
			node.put(c, line.substring(i));
			//结束递归
			return;
		//有key对应
		} else {
			//取出key对应的value
			Object o = node.get(c);
			//如果当前value是字符串,可能是"..."或者""
			if (o instanceof String) {
				//有两个一样的词，直接返回
				if ("".equals(o) && i == line.length()) {
					return;
				//两种情况，一种o = "..."， 或者  i != line.length
				} else {
					//先替换掉value为map
					Map v = new HashMap();
					node.put(c, v);
					//把原有的o建立到v中
					createIndexNode((String)o, 0, v);
					//在v中继续建立当前的Line
					createIndexNode(line, i, v);
				}
			//如果当前value是map,那么有多词进入递归
			} else {
				createIndexNode(line, i, (Map)o);
			}
		}
	}
	
	
	//打印index的方法
	private static final String[] PREFIX = {"", " ", "  ", "   ", "    ", "     ", "      ", "       ", "        ", "         ", "          "};
	@SuppressWarnings({ "rawtypes" })
	public static void printDictIndex(Map index, int l, PrintStream out) {
		for (Object o : index.keySet()) {
			Object v = index.get(o);
			out.println(PREFIX[l] + o);
			if (v instanceof String) {
				out.println(PREFIX[l+1] + v);
			} else {
				printDictIndex((Map)v, l+1, out);
			}
		}
	}
	
	//处理字典文件用
	public static void dealDictFile(File f) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "Unicode"));
		BufferedWriter out = new BufferedWriter(new FileWriter("C:\\1.txt"));
		String line = null;
		while ((line = in.readLine()) != null) {
			line = line.substring(0, line.indexOf(' '));
			out.append(line);
			out.newLine();
		}
		in.close();
		out.flush();
		out.close();
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		DictIndex d = new DictIndex("C:\\users\\study\\desktop\\moive_dict.txt");
		printDictIndex(d.dictIndex, 0, System.out);
	}

}
